<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="${mapperXmlPackageName}.repository.${className}Mapper">
    
    <sql id="SQL_${className?upper_case}_COLUMN" >
        <#list columns as column>
        	<#if !column_has_next>
        		${className?lower_case}.${column.dbColumName} as ${column.columName}
            </#if>
            <#if column_has_next>
        		${className?lower_case}.${column.dbColumName} as ${column.columName},
            </#if>
        </#list>
    </sql>
    
    <sql id="SQL_${className?upper_case}_WHERE">
    	<#list columns as column>
        	<if test="${column.columName} != null ">
	            AND ${className?lower_case}.`${column.dbColumName}` = #${r"{"} ${column.columName} ${r"}"}
	        </if>
        </#list>
        
    </sql>
    
	<select id="getList" parameterType="${mapperXmlPackageName}.bean.${className}Bean" resultType="${mapperXmlPackageName}.bean.${className}Bean">
        select
        <include refid="SQL_${className?upper_case}_COLUMN"/>
        from ${tableName} ${className?lower_case}
        <where>
            <include refid="SQL_${className?upper_case}_WHERE"/>
        </where>
    </select>
    
     <select id="getListCount" parameterType="${mapperXmlPackageName}.bean.${className}Bean" resultType="java.lang.Integer">
        select count(*) as counts
        from ${tableName} ${className?lower_case}
        <where>
            <include refid="SQL_${className?upper_case}_WHERE"/>
        </where>
    </select>
    
    <insert id="insert${className}" parameterType="${mapperXmlPackageName}.bean.${className}Bean">
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
	        <#list columns as column>
	            <#if !column_has_next>
        			<if test="${column.columName}!= null ">
		                ${column.dbColumName}
		            </if>
	            </#if>
	            <#if column_has_next>
	        		<if test="${column.columName}!= null ">
		                ${column.dbColumName},
		            </if>
	            </#if>
	        </#list>
            
            
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
           	<#list columns as column>
           		
	            <#if !column_has_next>
        			<if test="${column.columName} != null ">
		        	 	#${r"{"} ${column.columName} ${r"}"}
		            </if>
	            </#if>
	            <#if column_has_next>
	        		<if test="${column.columName} != null ">
		        	 	#${r"{"} ${column.columName} ${r"}"},
		            </if>
	            </#if>
	        </#list>
        </trim>
    </insert>
    
    <select id="get${className}Detail" parameterType="java.lang.String" resultType="${mapperXmlPackageName}.bean.${className}Bean">
    	 select
        <include refid="SQL_${className?upper_case}_COLUMN"/>
        from ${tableName} ${className?lower_case}
        <where>
        	${className?lower_case}.${primaryKey.dbColumName} = #${r"{"} id ${r"}"}
        </where>
    </select>
    
    <update id="update${className}" parameterType="${mapperXmlPackageName}.bean.${className}Bean">
		update ${tableName}
		<set>
            <#list columns as column>
		        	
		            <#if !column_has_next>
	        			<if test="${column.columName}!= null ">
			                ${column.dbColumName} = #${r"{"} ${column.columName} ${r"}"}
			            </if>
		            </#if>
		            <#if column_has_next>
		        		<if test="${column.columName}!= null ">
			                ${column.dbColumName} = #${r"{"} ${column.columName} ${r"}"},
			            </if>
		            </#if>
	        </#list>
		</set>
		where ${primaryKey.dbColumName} = #${r"{"} ${primaryKey.columName} ${r"}"}
	</update>
	
	<delete id="delete${className}" parameterType="${mapperXmlPackageName}.bean.${className}Bean">
		delete from ${tableName} 
		where ${primaryKey.dbColumName} = #${r"{"} ${primaryKey.columName} ${r"}"}
	</delete>
</mapper>
